package gui.control;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

public class ControlRecomendation {

	public StandardAnalyzer analyzer;
	public Directory diskDir;
	public IndexWriterConfig config;
	public IndexWriter w;
	
	public void openIndex(String indexDir) throws IOException{
		this.analyzer = new StandardAnalyzer(Version.LUCENE_35);
		File path=new File(indexDir);
		this.diskDir=FSDirectory.open(path); 
		this.config = new IndexWriterConfig(Version.LUCENE_35, analyzer);
	}
	
	public void showResults(Query q,String expectedField) throws Exception{
		//Building the scoring
		int hitsPerPage = 20;
		IndexReader reader = IndexReader.open(diskDir);
		IndexSearcher searcher = new IndexSearcher(reader);
		TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
		searcher.search(q, collector);
		ScoreDoc[] hits = collector.topDocs().scoreDocs;
		//Showing
		String[] queryResultsList=new String[hits.length];
		Document d;
		for(int i=0;i<hits.length;++i) {
			int docId = hits[i].doc;
			d= searcher.doc(docId);
			queryResultsList[i]=d.get(expectedField);
		}
	}
	public void executeQuery(String qString,String qField) throws ParseException{
		String qExpectedField=qField;
		Query q = new QueryParser(Version.LUCENE_35,qField,analyzer).parse(qString);
	}
}
